layui.define(['jquery'],
    function (exports) {
        var $ = layui.jquery;
        var kaPreviewText = new function () {
            // 初始化 Set
            this.init = function (id) {
                if (id == undefined) {
                    return false;
                }
                var values = this.getPreviewTextvalues();
                if (values == null) {
                    values = [];
                    values.push({
                        id: id,
                        time: new Date().getTime()
                    });
                    localStorage.setItem("kaPreviewTextValuesId", JSON.stringify(values));
                    return true;
                } else {
                    var boolean = false;
                    for (var i in values) {
                        var value = values[i];
                        if (value.id == id) {
                            boolean = true;
                            break;
                        }
                    }
                    return boolean;
                }
            }
            // getKaPreviewTextValues
            this.getPreviewTextvalues = function () {
                var kaPreviewTextValuesId = localStorage.getItem("kaPreviewTextValuesId");
                if (kaPreviewTextValuesId == undefined || kaPreviewTextValuesId == null || kaPreviewTextValuesId == "") {
                    return null;
                } else {
                    var dataArray = JSON.parse(kaPreviewTextValuesId);
                    return dataArray;
                }
            }
            // 初始化值 id 内容 类型（1: 追加, 2: 覆盖）
            this.set = function (id, content, type) {
                var values = this.getPreviewTextvalues();
                if (values == null) {
                    return false
                } else {
                    var boolean = false;
                    for (var i in values) {
                        var value = values[i];
                        if (value.id == id) {
                            boolean = true;
                            break;
                        }
                    }
                    if (!boolean) {
                        return boolean;
                    }
                }
                if (id == undefined || id == null || id == "") {
                    console.log("kaPreviewText", "Id 内容不能为空！");
                    return false
                }
                if (type == undefined || type == null || type == "") {
                    type = 1;
                }
                if (content == undefined || content == null || content == "") {
                    var device = layui.device(key);
                    content = device.os;
                }
                var data = {
                    id: id,
                    content: content,
                    type: type
                }
                localStorage.setItem(id, JSON.stringify(data));
            }
            // 删除
            this.delete = function (id) {
                if (top.kaPreviewTextValues == undefined || top.kaPreviewTextValues == null) {
                    return false
                } else {
                    var kaPreviewTextMap = top.kaPreviewTextValues.get(id);
                    if (kaPreviewTextMap == undefined || kaPreviewTextMap == null || kaPreviewTextMap == "") {
                        return false
                    } else {
                        top.kaPreviewTextValues.delete(id);
                    }
                }
            }
            // 打开
            this.open = function (id) {
                this.init(id);// 初始化
                window.open("previewText.html?id=" + id, "_blank", "width=800,height=800");
            }
        };
        exports('kaPreviewText', kaPreviewText);
    }
)